package com.liu.controller;

import com.liu.entity.HosDep;
import com.liu.service.HosDepService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (HosDep)表控制层
 *
 * @author makejava
 * @since 2023-03-06 20:56:37
 */
@RestController
@RequestMapping("/hosDep")
@CrossOrigin
@Slf4j
public class HosDepController {
    /**
     * 服务对象
     */
    private HosDepService hosDepService;

    @Autowired
    @Qualifier("hosDepServiceImpl")
    public void setService(HosDepService hosDepService) {
        this.hosDepService = hosDepService;
    }

    /**
     * 分页查询
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public Map<String, Object> findPage(@RequestParam Integer pageNum,
                                        @RequestParam Integer pageSize
                                        ) {
        pageNum = (pageNum - 1) * pageSize;
        List<HosDep> data = hosDepService.queryAllByLimit(pageNum, pageSize);
        Long total = hosDepService.count();
        Map<String, Object> res = new HashMap<>();
        res.put("data", data);
        res.put("total", total);
        return res;
    }

    /**
     * 查询全部数据
     *
     * @return
     */
    @GetMapping("/list")
    public List<HosDep> findAll() {
        return this.hosDepService.findAll();
    }

    /**
     * 添加或更新
     *
     * @param hosDep
     */
    @PostMapping("/edit")
    public int insert(@RequestBody HosDep hosDep) {
        log.info("当前保存或修改的信息:{}", hosDep.toString());
        //根据传递过来的参数studentId是否存在来决定进入哪个方法
        if (hosDep.getId() != null && hosDep.getId().toString().length() > 0) {
            System.out.println(hosDep.getId());
            return hosDepService.update(hosDep);
        } else {
            System.out.println(hosDep.getId());
            return hosDepService.insert(hosDep);
        }
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/del")
    public int delete(Integer id) {
        log.info("当前删除数据的id:{}", id);
        return hosDepService.deleteById(id);
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping("/delBatch")
    public int deleteBatch(@RequestBody List<Integer> ids) {
        return hosDepService.deleteByIds(ids);
    }

    /**
     * 根据id查找
     *
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public HosDep queryById(Integer id) {
        return hosDepService.queryById(id);
    }

}

